<?php
/**
 * Copyright (c) 不争网络
 *
 * @Description     未登录-基础框架.
 * @Author          zhangyong QQ:2315918189
 * @Copyright       Copyright (c) 不争网络 保留所有版权()
 * @Date:           2018/7/19 0019
 * @CreateBy        PhpStorm.
 */
namespace app\common\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Image;
class Base extends Controller{
    protected $post;
    public function _initialize(){
        $this->post  = $this->request->post();
        $this->host  = $this->request->server('REQUEST_SCHEME') . '://' . $this->request->host();
        $this->get   = $this->request->get();
        $p = input('p',1,'intval');
        if($p < 1){
            $p = 1;
        }
        $this->p = $p;
    }



    /**
     * 接口返回封装
     */
    protected function apiReturn($status='-1',$msg,$data=[]){
        if($status == '-1'){
            exit('参数调用错误');
        }
        exit(json_encode(array('code'=>$status,'msg'=>$msg,'data'=>$data)));
    }

    /**
     * 通用上传
     */
    public function uploads($path='images',$name = 'file')
    {
        $path = $path;
        $file = $this->request->file($name);
        if ($file) {
            $move = ROOT_PATH . 'public' . DS . 'upload' . DS . $path;
            $info = $file->rule('md5')->move($move);
            if ($info) {
                $src      = $move . DS . $info->getSaveName();
                $if_image = getimagesize($src);
               
                $return = ['src' => str_replace('\\', '/', str_replace(ROOT_PATH . 'public', $this->host, $src))];
                
                $this->apiReturn(1,'上传成功', $return);
            }
            $this->apiReturn(0,$file->getError());
        }
        $this->apiReturn(0,'上传失败,请稍后重试');
    }
}